package site.xuxingcan.messageclient;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by xuxingcan on 2016/7/28.
 */
public class MessageActivity extends Activity {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");// 输出北京时间

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);
        ListView listView = (ListView) findViewById(R.id.list_view);
        MyAdapter mAdapter = new MyAdapter();
        mAdapter.setMessageModelArrayList(FileUtil.getInstance(this).getMessageModelArrayList());
        listView.setAdapter(mAdapter);
    }

    class MyAdapter extends BaseAdapter {
        private ArrayList<MessageModel> messageModelArrayList;

        public void setMessageModelArrayList(ArrayList<MessageModel> messageModelArrayList) {
            this.messageModelArrayList = messageModelArrayList;
        }

        @Override
        public int getCount() {
            return messageModelArrayList.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View convertView, ViewGroup viewGroup) {
            ViewHolder mViewHolder;
            if (convertView == null) {
                mViewHolder = new ViewHolder();
                convertView = LayoutInflater.from(getApplication()).inflate(R.layout.message_item, viewGroup, false);
                mViewHolder.time = (TextView) convertView.findViewById(R.id.time);
                mViewHolder.phone = (TextView) convertView.findViewById(R.id.phone);
                mViewHolder.key = (TextView) convertView.findViewById(R.id.key);
                convertView.setTag(mViewHolder);
            } else {
                mViewHolder = (ViewHolder) convertView.getTag();
            }
            MessageModel messageModel = messageModelArrayList.get(i);
            mViewHolder.time.setText(sdf.format(new Date(messageModel.time)));
            mViewHolder.phone.setText(messageModel.phone);
            mViewHolder.key.setText(messageModel.key);
            return convertView;
        }
    }

    class ViewHolder {
        TextView time, phone, key;
    }
}
